<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>添加demo</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">

	<link rel="stylesheet" href="../../../static/layui2/css/layui.css" media="all" />
	<link rel="stylesheet" href="../../../static/css/font_eolqem241z66flxr.css" media="all" />
</head>
<body class="childrenBody">
	<form class="layui-form" lay-filter="demo">
		<div class="layui-form-item">
			<label class="layui-form-label">demo标题</label>
			<div class="layui-input-block">
				<input type="text" name="name"  class="layui-input newsName" lay-verify="required" placeholder="请输入demo">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">内容摘要</label>
			<div class="layui-input-block">
				<textarea placeholder="请输入内容摘要"  name="desc" class="layui-textarea"></textarea>
			</div>
		</div>



		<div class="layui-form-item">
			<label class="layui-form-label">上传图片</label>
			<div class="layui-input-block">
				<button type="button" class="layui-btn" id="test1">
					<i class="layui-icon">&#xe67c;</i>上传图片
				</button>
			</div>
		</div>



		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn" lay-submit=""  type="button"  lay-filter="formData">立即提交</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
		    </div>
		</div>
	</form>
	<script type="text/javascript" src="../../../static/layui2/layui.js"></script>
    <script type="text/javascript" src="../../../static/js/queryUtils.js"></script>
<!--	<script type="text/javascript" src="demoAdd.js"></script>-->

	<script>
		layui.use(['jquery', 'form', 'laydate','upload'], function () {
			var form = layui.form
					, layer = layui.layer
					, laydate = layui.laydate
					, $ = layui.jquery
			        ,upload = layui.upload;
			var operateType = QueryUtils.GetQueryString("type");
			var id = QueryUtils.GetQueryString("id");
			var active = {
				cancel: function () {
					// 当你在iframe页面关闭自身时
					var index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
					parent.layer.close(index); // 再执行关闭
				}
			};

			$('.operate-button-group .layui-btn').on('click', function () {
				var type = $(this).data('type');
				active[type] ? active[type].call(this) : '';
			});


			//执行实例
			var uploadInst = upload.render({
				elem: '#test1' //绑定元素
				,url: '/upload/' //上传接口
				,done: function(res){
					//上传完毕回调
				}
				,error: function(){
					//请求异常回调
				}
			});

			switch (operateType) {
				case "edit":
					$.ajax({
						url: "/demoController/view",
						data: {id: id},
						dataType: "json",
						type: "post",
						async: false,
						success: function (res) {

							if (res.code === "00") {
									//表单初始赋值
									form.val('demo', {
										"name": res.data.name
										,"desc": res.data.desc
									})

								form.render();
							} else if (res.code === "01") {
								layer.msg(res.msg, function () {
								});
							}
						},
						error: function (XMLHttpRequest, textStatus, errorThrown) {
							console.error(XMLHttpRequest.status);
							console.error(XMLHttpRequest.readyState);
							console.error(textStatus);
						}
					});
					break;

			}




			//监听提交
			form.on('submit(formData)', function (data) {
				/*layer.alert(JSON.stringify(data.field), {
                    title: '最终的提交信息'
                });*/
				if (operateType === "add") {
					layer.msg('确认保存信息吗', {
						time: 0
						, btn: ['确定', '取消']
						, shade: 0.3
						, yes: function (index) {
							layer.close(index); // 关闭当前提示框
							$.ajax({
								url: "/demoController/add",
								data: data.field,
								dataType: "json",
								type: "post",
								async: false,
								success: function (res) {

									if (res.code === "00") {
										var i = parent.layer.getFrameIndex(window.name);
										parent.layer.close(i);  // 关闭新建页面
										parent.layui.table.reload('dataTable', {});    // 父页面表格刷新
										parent.layer.msg("保存成功", {offset: '150px'});   // 父页面弹出保存成功
									} else if (res.code === "01") {
										layer.msg(res.msg, function () {
										});
									}
								},
								error: function (XMLHttpRequest, textStatus, errorThrown) {
									console.error(XMLHttpRequest.status);
									console.error(XMLHttpRequest.readyState);
									console.error(textStatus);
								}
							});
						}
					});
				}
				else if (operateType === "edit") {
					data.field.id = id;
					layer.msg('确认保存信息吗', {
						time: 0
						, btn: ['确定', '取消']
						, shade: 0.3
						, yes: function (index) {
							layer.close(index); // 关闭当前提示框
							$.ajax({
								url: "/demoController/update",
								data: data.field,
								dataType: "json",
								type: "post",
								async: false,
								success: function (res) {

									if (res.code === "00") {
										var i = parent.layer.getFrameIndex(window.name);
										parent.layer.close(i);  // 关闭新建页面
										parent.layui.table.reload('dataTable', {});    // 父页面表格刷新
										parent.layer.msg("保存成功", {offset: '150px'});   // 父页面弹出保存成功
									} else if (res.code === "01") {
										layer.msg(res.msg, function () {
										});
									}
								},
								error: function (XMLHttpRequest, textStatus, errorThrown) {
									console.error(XMLHttpRequest.status);
									console.error(XMLHttpRequest.readyState);
									console.error(textStatus);
								}
							});
						}
					});
				}
				return false;
			});

		});
	</script>
</body>
</html>